//
//  NewFeatureCollectionViewController.swift
//  JKWeiBo
//
//  Created by 王冲 on 2017/11/19.
//  Copyright © 2017年 希爱欧科技有限公司. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewFeatureCollectionViewController: UICollectionViewController {

    private let pageCount = 3
    private var layout: UICollectionViewFlowLayout = NewFeaturelayout()
    // 因为系统初始化的构造方法是带参数的(UICollectionViewFlowLayout) 而不是不带参数的，所以不用加 override
    init(){
        
        super.init(collectionViewLayout: layout)
        self.collectionView?.backgroundColor = UIColor.white
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewWillAppear(_ animated: Bool) {
        
        super.viewWillAppear(animated)
        /*
        //1.设置layout的布局
        layout.itemSize = UIScreen.main.bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.horizontal
        // 2.设置collectionView的属性
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        collectionView?.isPagingEnabled = true
        */
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 1.注册一个cell
        collectionView?.register(newFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    
    // MARK: UICollectionViewDataSource UICollectionViewDelegate
    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        
        return 1
    }
    // 返回一共有多少个cell
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pageCount
    }
    // cell的重写
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        // 1.获取cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! newFeatureCell
        // 2.设置cell的数据
        cell.ImageIndex = indexPath.item + 1
        // 3.返回cell
        return cell
    }
    
    override func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        // 传递给我们的是上一页的索引
        let path = collectionView.indexPathsForVisibleItems.last
        print(path!)
    }
    
    
    
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if indexPath.item == pageCount-1 {
            
              print("您点击的是租后一页,页码是第\(indexPath.item+1)页")
              // 引导图之后进行跳入主控制器
              NotificationCenter.default.post(name: NSNotification.Name(rawValue: switchRootViewController), object: true)
        }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

// MARK: 自定义cell swift的一个文件里面可以定义多个类的
private class newFeatureCell: UICollectionViewCell
{
    // 保存图片的索引
    // 在swift里面,private修饰的东西如果是在同一个文件下是可以被调用的
    var ImageIndex:Int?{
        
        didSet{
           
            iconView.image = UIImage(named:"guide_\(ImageIndex!).jpg")
        }
    }
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        // 1.初始化UI
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        // 1.添加子控件到 UICollectionView 上
        contentView.addSubview(iconView)
        // 2.布局子控件的位置
        iconView.frame = contentView.frame
    }
    
    // MARK:- 懒加载
    private lazy var iconView: UIImageView = {
        
        let iconViewImage = UIImageView()
        iconViewImage.contentMode = UIViewContentMode.scaleAspectFill
        iconViewImage.clipsToBounds = true
        return iconViewImage
    }()
}

// MARK: 重写layout方法
private class NewFeaturelayout: UICollectionViewFlowLayout {
    
    override func prepare() {
        // 1.准备布局
        // 什么时候调用？ 1.先调用一个有 多少cell 2.调用准备布局，3.调用返回cell
        itemSize = UIScreen.main.bounds.size
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.horizontal
        // 2.设置collectionView的属性
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        collectionView?.isPagingEnabled = true
    }
}


